package com.nlscan.qdkit.ui.publico.util;


import com.quvii.qvlib.constant.QvTimeConstants;
import com.quvii.qvlib.util.QvTimeUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 日期工具类
 * Created by QING on 2018/1/29.
 * 与业务相关
 */

public class DateUtil {


    /**
     * 日期格式，年月日，用横杠分开，例如：2008-08-08
     */
    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";


    /**
     * 格式化Date时间
     *
     * @param time       Date类型时间
     * @param timeFromat String类型格式
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFromat) {
        return QvTimeUtils.date2String(time, timeFromat);
    }


    /**
     * 获得指定日期的前一天
     *
     * @param date
     * @return
     */
    public static Date getSpecifiedDayBefore(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day - 1);
        return calendar.getTime();
    }

    /**
     * 获得指定日期的后一天
     *
     * @param date
     * @return
     */
    public static Date getSpecifiedDayAfter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day + 1);
        return calendar.getTime();
    }


    /**
     * 将时间转化为年月日格式，比如 2018 - 1- 17
     *
     * @param date
     * @return
     */
    public static String converToYearMonthAndDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        return year + " - " + month + " - " + day;
    }

    /**
     * 通过Date 获取对应的Calendar
     *
     * @param date
     * @return
     */
    public static Calendar getCalendarByDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }


    /*********  与业务相关 BEGIN ********/

    /**
     * 将传入的时间转化为xml格式的起始时间戳
     * 如 2018-01-15t00:00:05z
     * 注：因为从00:00:00 会搜到前一天的录像，所以从5s后开始搜索
     *
     * @param date
     * @return
     */
    public static String convertToXmlStartTime(Date date) {
        String dateStr = parseDateToStr(date, DATE_FORMAT_YYYY_MM_DD);
        return dateStr + "t00:00:05z";
    }


    /**
     * 将传入的时间转化为xml格式的结束时间戳
     * 如 2018-01-15tt23:59:55z
     *
     * @param date
     * @return
     */
    public static String convertToXmlEndTime(Date date) {
        String dateStr = parseDateToStr(date, DATE_FORMAT_YYYY_MM_DD);
        return dateStr + "t23:59:55z";
    }

    public static String convertToXmlTime(Date date) {
        return parseDateToStr(date, "yyyy-MM-dd't'HH:mm:ss'z'");
    }


    /**
     * 按照日期排序
     */
//    public static class ComparatorDate implements Comparator {
//
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//
//        public int compare(Object obj1, Object obj2) {
//            Messages t1 = (Messages) obj1;
//            Messages t2 = (Messages) obj2;
//            Date d1, d2;
//            try {
//                d1 = format.parse(t1.getDate());
//                d2 = format.parse(t2.getDate());
//            } catch (ParseException e) {
//                return 0;
//            }
//            if (d1.before(d2)) {
//                return 1;
//            } else {
//                return -1;
//            }
//        }
//    }

    /**
     * 获取当前时区
     *
     * @return
     */
    public static String getCurrentTimeZone() {
        TimeZone tz = TimeZone.getDefault();
        String strTz = tz.getDisplayName(false, TimeZone.SHORT);
        return strTz;

    }

    /*********  与业务相关 END ********/


    public static String toStandFormat(int year, int month, int day) {
        return toStandFormat(year, month, day, 0, 0, QvTimeConstants.DATE_FORMAT_YYYY_MM_DD);
    }

    /**
     * @param month (1-12)
     */
    public static String toStandFormat(int year, int month, int day, int hour, int minute) {
        return toStandFormat(year, month, day, hour, minute, QvTimeConstants.DATE_FORMAT_YYYY_MM_DD_HH_MM);
    }

    /**
     * @param month (1-12)
     */
    public static String toStandFormat(int year, int month, int day, int hour, int minute, final String format) {
        String strYear = "" + year;
        String strMonth = month < 10 ? "0" + month : "" + month;
        String strDay = day < 10 ? "0" + day : "" + day;
        String strHour = hour < 10 ? "0" + hour : "" + hour;
        String strMinute = minute < 10 ? "0" + minute : "" + minute;
        String strTime = null;
        switch (format) {
            case QvTimeConstants.DATE_FORMAT_YYYY_MM_DD:
                strTime = strYear + "-" + strMonth + "-" + strDay;
                break;
            case QvTimeConstants.DATE_FORMAT_YYYY_MM_DD_HH_MM:
                strTime = strYear + "-" + strMonth + "-" + strDay
                        + " " + strHour + ":" + strMinute;
                break;
        }
        return strTime;
    }


}
